package com.heiyu.task;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.heiyu.constants.PushStatus;
import com.heiyu.dao.*;
import com.heiyu.entity.ClientPushInfo;
import com.heiyu.entity.GlobalShowInfo;
import com.heiyu.entity.UserPushInfo;
import com.heiyu.utils.CacheKey;
import com.heiyu.utils.RangeUtil;
import com.heiyu.utils.RedisUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 单币推送消息
 */
//@Component
public class CoinRangePushTask {

    @Autowired
    private UserFollowMapper userFollowMapper;
    @Autowired
    private ShowFull30minMapper showFull30minMapper;
    @Autowired
    private UserPushInfoMapper userPushInfoMapper;
    @Autowired
    private ClientPushInfoMapper clientPushInfoMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;

    @Scheduled(fixedDelay = 10000)
    public void execute(){
        StringBuilder target = new StringBuilder();
        target.append("#5a22430dce79d2cf9b87bc4d#").append("5a22430dce79d2cf9b87bc29#").append("5a22430dce79d2cf9b87bc2c#").append("5a22430dce79d2cf9b87bc35#");
        List<String> targetIds = Lists.newArrayList();
        targetIds.add("5a22430dce79d2cf9b87bc4d");
        targetIds.add("5a22430dce79d2cf9b87bc29");
        targetIds.add("5a22430dce79d2cf9b87bc2c");
        targetIds.add("5a22430dce79d2cf9b87bc35");
        List<String> followTarget = this.userFollowMapper.findAllFollowCoin();
        if(CollectionUtils.isNotEmpty(followTarget)){
            for(String targetId:followTarget){
                if(target.toString().contains("#"+targetId+"#")){
                    continue;
                }
                target.append(targetId).append("#");
                targetIds.add(targetId);
            }
        }
        //查询单个币种30分钟内的数据
        int i=0;
        List<ClientPushInfo> clientPushInfoList;
        StringBuilder iosUsers;
        StringBuilder androidUsers;
        UserPushInfo userPushInfo;
        GlobalShowInfo globalShowInfo;
        Map<String,String> param;
        for(String s:targetIds){
            i++;
            if(StringUtils.isNoneBlank(redisUtils.get(CacheKey.PUSH_STOP+s))){
                continue;
            }

            globalShowInfo = new GlobalShowInfo();
            globalShowInfo.setTargetId(s);
            try{
                globalShowInfo = this.globalShowInfoMapper.selectList(globalShowInfo).get(0);
                if(globalShowInfo == null||Math.abs(globalShowInfo.getPercentChange24h().doubleValue())<=10){
                    continue;
                }
            }catch (Exception e){
                continue;
            }
            userPushInfo  = new UserPushInfo();
            String updown = globalShowInfo.getPercentChange24h().doubleValue()>0?"涨幅":"跌幅";
            userPushInfo.setTitle("【行情雷达】");
            userPushInfo.setContent("当前"+globalShowInfo.getSymbol()+"价格：¥"+globalShowInfo.getPriceCny()+"（"+updown+" " +
                    ""+RangeUtil.formatter(globalShowInfo.getPercentChange24h()) +"），价格震动较大");
            //全局推送
            if(i<=4){
                clientPushInfoList = this.clientPushInfoMapper.listNotClose();
            }else{
                clientPushInfoList = this.clientPushInfoMapper.listNotCloseTargetId(s);
            }


            if(CollectionUtils.isNotEmpty(clientPushInfoList)){
                //10分钟内不推送该类型的消息
                redisUtils.set(CacheKey.PUSH_STOP+s,"1",1800);
                iosUsers = new StringBuilder();
                androidUsers = new StringBuilder();
                for(ClientPushInfo pushInfo:clientPushInfoList){
                    if(pushInfo.getClientType().equals("1")){
                        iosUsers.append(pushInfo.getPushId()).append(",");
                    }else{
                        androidUsers.append(pushInfo.getPushId()).append(",");
                    }
                }

                userPushInfo.setStatus(PushStatus.WAIT_PUSH);
                userPushInfo.setSendTime(new Date());
                userPushInfo.setIsDeleted("0");
                userPushInfo.setCreatedTime(new Date());
                userPushInfo.setModifyTime(userPushInfo.getCreatedTime());
                param = Maps.newHashMap();
                param.put("schema","schema://heiyu.com/market/detail?type=01&targetId="+s);
                userPushInfo.setExt(JSON.toJSONString(param));
                //构造推送信息
                if(StringUtils.isNoneBlank(iosUsers.toString())){
                        userPushInfo.setClientType("1");
                        userPushInfo.setPushIds(iosUsers.toString().substring(0,iosUsers.length()-1));
                        this.userPushInfoMapper.insert(userPushInfo);
                }
                if(StringUtils.isNoneBlank(androidUsers.toString())){
                        userPushInfo.setClientType("2");
                        userPushInfo.setPushIds(androidUsers.toString().substring(0,androidUsers.length()-1));
                        userPushInfo.setId(null);
                        userPushInfoMapper.insert(userPushInfo);
                }
            }
        }

    }

}
